package com.scut.filedownloader.ui.adapter;


import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.UiThread;
import androidx.recyclerview.widget.RecyclerView;

import com.scut.filedownloader.R;
import com.scut.filedownloader.util.MessageBuilder;

import java.util.ArrayList;
import java.util.List;


/**
 * 循环视图线性布局是适配器,该适配器通过访问自己定义的ItemData接口来获取数据
 */
public class RecyclerViewLinearAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    protected List<ItemData> listOfItems=null;
    protected Handler mHandler=new Handler(Looper.getMainLooper()){
        @Override
        public void handleMessage(@NonNull Message msg) {
            switch (msg.what){
                case UIMessageCode.NOTIFY_ALL:
                    notifyDataSetChanged();
                    break;
                case UIMessageCode.NOTIFY_CHANGE:
                    //暂时看看能不能在另一端更改信息
                    break;
                case UIMessageCode.NOTIFY_REMOVE:
                    notifyItemRemoved(msg.arg1);
                    break;
                case UIMessageCode.NOTIFY_INSERT:
                    notifyItemInserted(msg.arg1);
                default:
                    super.handleMessage(msg);
                    break;
            }
        }
    };

    /**
     * 传入一个包含ItemData元素的列表，以创建适配器对象
     * @param listOfItems
     */
    public RecyclerViewLinearAdapter(List<ItemData> listOfItems){
        if(listOfItems==null){
            this.listOfItems=new ArrayList<>();
        }
        else{
            this.listOfItems=listOfItems;
        }
    }

    /**
     * 向该适配器中，添加单个数据，该方法可以在非ui线程中执行,以获得视图显示同步
     * @param item
     */
    public void addItem(ItemData item){
        listOfItems.add(item);
        mHandler.sendMessage(MessageBuilder.obtain(UIMessageCode.NOTIFY_INSERT,listOfItems.size()-1));
    }

    /**
     * 移除适配器中对应位置的数据项，position应该由ViewHolder.getAdapterPosition获得
     * @param position
     */
    public void removeItem(int position){
        if(position<0||position>listOfItems.size()-1)
            return; //out of range
        listOfItems.remove(position);
        mHandler.sendMessage(MessageBuilder.obtain(UIMessageCode.NOTIFY_REMOVE,position));
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View itemView= LayoutInflater.from(parent.getContext()).inflate(R.layout.task_layout,parent,false);
        return new ViewHolderProxy(itemView);
    }



    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        ViewHolderProxy viewHolderProxy= (ViewHolderProxy) holder;
        if(position>getItemCount()-1||position<0){
            return; //position out of range
        }
        ItemData data=listOfItems.get(position);
        data.bindViewHolder(viewHolderProxy,position); //do all update ui work in it

    }

    /**
     * ItemData接口只可以适配一种类型的列表项布局, 不同列表项所需要的数据不尽相同，但都应该从该接口中取得
     */
    public interface ItemData{
        String getTitle();
        String getURL();
        String getProgressDesc();
        ViewHolderProxy getBoundViewHolder();
        @UiThread
        void bindViewHolder(ViewHolderProxy holder,int position);
    }

    @Override
    public int getItemCount() {
        return listOfItems.size();
    }

    public class ViewHolderProxy extends RecyclerView.ViewHolder{

        public View mView;
        public TextView tv_url;
        public TextView tv_title;
        public TextView tv_progress_desc;
        public ProgressBar progressBar;
        public Button firstBtn;
        public Button secondBtn;

        public ViewHolderProxy(@NonNull View itemView) {
            super(itemView);
            mView=itemView;
            tv_url=mView.findViewById(R.id.tv_src_url);
            tv_title=mView.findViewById(R.id.tv_task_title);
            tv_progress_desc =mView.findViewById(R.id.tv_progress_desc);

            progressBar=mView.findViewById(R.id.progress_horizontal);
            firstBtn=mView.findViewById(R.id.first_btn);
            secondBtn=mView.findViewById(R.id.second_btn);

        }
    }

    final static class UIMessageCode{
        final static int NOTIFY_REMOVE=0;
        final static int NOTIFY_ALL=1;
        final static int NOTIFY_INSERT=2; //arg1=position
        final static int NOTIFY_CHANGE=3; //arg1=position

    }

}
